package com.ipan.poi.utils;

import java.io.File;
import java.io.IOException;

/**
 * 文件工具类；
 * 
 * @author iPan
 * @version 2013-05-29
 */
public class PoiFileHelper extends org.apache.commons.io.FileUtils {

	public static boolean createFile(String name, int index) {
		String name1 = name.substring(0, index);
		File file1 = new File(name1);
		boolean ret = false;
		if (file1.exists()) {
			return true;
		}
		int i;
		for (i = index - 1; i > 1; --i) {
			if (name.charAt(i) == '/' || name.charAt(i) == '\\') {
				break;
			}
		}
		if (i > 1) {
			ret = createFile(name, i);
		}
		String name2 = name.substring(0, index);
		File file2 = new File(name2);
		if (!file2.exists()) {
			if (name.length() == index && name.indexOf('.') != -1) {
				try {
					ret = file2.createNewFile();
				} catch (IOException e) {
					throw new RuntimeException("创建文件[" + name + "]失败！", e);
				}
			} else {
				ret = file2.mkdir();
			}
		}
		return ret;
	}
	
	public static String getFileExtension(String filePath) {
		int index = filePath.lastIndexOf('.');
		String extension = (index < 0) ? filePath : filePath.substring(index + 1).toLowerCase();
		return extension;
	}
	
	public static boolean checkFileExtension(String filePath, String checkExtension) {
		String extension = getFileExtension(filePath);
		return extension.equalsIgnoreCase(checkExtension);
	}
}
